package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.entity.TmsCar;
import com.ky.tms.entity.TmsCarInsure;
import com.ky.tms.entity.TmsCarOil;
import com.ky.tms.entity.TmsOil;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.mapper.TmsCarInsureMapper;
import com.ky.tms.mapper.TmsCarMapper;
import com.ky.tms.mapper.TmsCarOilMapper;
import com.ky.tms.mapper.TmsOilMapper;
import com.ky.tms.service.ITmsCarService;
import com.ky.tms.util.RequestContextHolderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-05-13
 */
@Service
public class TmsCarServiceImpl extends ServiceImpl<TmsCarMapper, TmsCar> implements ITmsCarService {

    @Autowired
    private TmsCarMapper tmsCarMapper;

    @Autowired
    private TmsCarOilMapper tmsCarOilMapper;

    @Autowired
    private TmsOilMapper tmsOilMapper;

    @Autowired
    private TmsCarInsureMapper tmsCarInsureMapper;

    @Override
    public IPage<TmsCar> page(TmsCar tmsCar, Long limit, Long pages) {
        IPage<TmsCar> page = new Page(pages, limit);
        List<TmsCar> tmsCars = tmsCarMapper.carPage(page, tmsCar);
        page.setRecords(tmsCars);
        for(TmsCar tcar: page.getRecords()){
            if("oil".equals(tmsCar.getFlag()) || true){
                QueryWrapper<TmsCarOil> w = new QueryWrapper<>();
                w.eq("is_valid", 1);
                w.eq("car_no", tcar.getCarNo());
                w.orderByDesc("id");
                List<TmsCarOil> tmsCarOils = tmsCarOilMapper.selectList(w);
                tcar.setOils(tmsCarOils);
            }
            if("insure".equals(tmsCar.getFlag()) || true){
                QueryWrapper<TmsCarInsure> w = new QueryWrapper<>();
                w.eq("is_valid", 1);
                w.eq("car_no", tcar.getCarNo());
                w.orderByDesc("id");
                List<TmsCarInsure> tmsCarInsures = tmsCarInsureMapper.selectList(w);
                tcar.setInsures(tmsCarInsures);
            }
        }
        return page;
    }

    @Override
    public void add(TmsCar tmsCar) throws PredictableException {

        QueryWrapper<TmsCar> wrapper = new QueryWrapper<>();
        wrapper.eq("car_no", tmsCar.getCarNo());
        wrapper.eq("is_valid", 1);
        Integer integer = tmsCarMapper.selectCount(wrapper);
        if(integer>0){
            throw new PredictableException("车牌号重复");
        }
        String userName = RequestContextHolderUtil.getUserName();
        tmsCar.setIsLock(0);
        tmsCar.setIsValid(1);
        tmsCar.setCreateTime(LocalDateTime.now());
        tmsCar.setCreateUser(userName);
        tmsCarMapper.insert(tmsCar);
    }

    @Override
    public void delete(List<Long> ids) {
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public void update(TmsCar tmsCar) throws PredictableException {
        QueryWrapper<TmsCar> wrapper = new QueryWrapper<>();
        wrapper.eq("car_no", tmsCar.getCarNo());
        wrapper.eq("is_valid", 1);
        wrapper.ne("id", tmsCar.getId());
        Integer integer = tmsCarMapper.selectCount(wrapper);
        if(integer>0){
            throw new PredictableException("车牌号重复");
        }
        String userName = RequestContextHolderUtil.getUserName();
        tmsCar.setUpdateTime(LocalDateTime.now());
        tmsCar.setUpdateUser(userName);
        tmsCarMapper.updateById(tmsCar);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void oil(TmsCarOil tmsCarOil) throws PredictableException {

        BigDecimal subtract = tmsCarOil.getKilometresEnd().subtract(tmsCarOil.getKilometresStart());
        tmsCarOilMapper.updateKwh(tmsCarOil.getOilCount(), tmsCarOil.getCarNo());
        LocalDateTime now = LocalDateTime.now();
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarOil.setOilAmount(tmsCarOil.getOilPrice().multiply(tmsCarOil.getOilCount()));
        tmsCarOil.setIsValid(1);
        tmsCarOil.setIsLock(0);
        tmsCarOil.setCreateTime(now);
        tmsCarOil.setCreateUser(userName);
        tmsCarOil.setKwh(tmsCarOil.getOilCount().divide(subtract).multiply(new BigDecimal("100")));
        tmsCarOilMapper.insert(tmsCarOil);
        QueryWrapper<TmsCar> wrapper = new QueryWrapper<>();
        wrapper.eq("car_no",tmsCarOil.getCarNo());
        TmsCar tmsCar = new TmsCar();
        tmsCar.setKilometres(tmsCarOil.getKilometresEnd());
        tmsCarMapper.update(tmsCar, wrapper);

        QueryWrapper<TmsOil> wrapperOil = new QueryWrapper<>();
        wrapperOil.eq("oil_no", tmsCarOil.getOilNo());
        wrapperOil.eq("is_valid", 1);
        TmsOil tmsOil = tmsOilMapper.selectOne(wrapperOil);
        tmsOil.setOilAmount(tmsOil.getOilAmount().subtract(tmsCarOil.getOilOffAmount()));
        tmsOil.setUpdateTime(now);
        tmsOil.setUpdateUser(userName);
        tmsOilMapper.updateById(tmsOil);

    }

    @Override
    public void insure(TmsCarInsure tmsCarInsure) throws PredictableException {
        LocalDateTime now = LocalDateTime.now();
        String userName = RequestContextHolderUtil.getUserName();
        tmsCarInsure.setIsLock(0);
        tmsCarInsure.setIsValid(1);
        tmsCarInsure.setCreateTime(now);
        tmsCarInsure.setCreateUser(userName);
        tmsCarInsureMapper.insert(tmsCarInsure);
    }
}
